package com.humanres.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;

import com.ciko.code.base.BaseActicity.OnNavRightClickListener;
import com.ciko.code.base.BaseLabelActivity;
import com.ciko.code.fragment.ResMoreReciveFragment;
import com.ciko.code.fragment.ResMoreSendFragment;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.GetOverTimeFlowsBySelfEty;
import com.ciko.code.http.Req.GetOverTimeFlowsBySelfReq;
import com.ciko.code.http.Req.GetTripFlowsBySelfEty;
import com.ciko.code.http.Req.GetTripFlowsBySelfReq;
import com.humanres.http.Req.GetLeaveFlowsBySelfEty;
import com.humanres.http.Req.GetLeaveFlowsBySelfReq;
import com.mywork.R;
import com.mywork.WorkUIKit;

import java.util.ArrayList;
import java.util.List;

public class ResMoreActivity extends BaseLabelActivity implements OnNavRightClickListener {

    private String title;

    private List<Fragment> mFragmentList = new ArrayList<>();
//    private String[] mTitleArr = {"我发出的", "我审批的(1)"};
    ResMoreSendFragment resMoreSendFragment;
    ResMoreReciveFragment resMoreReciveFragment;
    boolean f=true;
    boolean second=false;

    private List<GetLeaveFlowsBySelfEty> getLeaveFlowsBySelfEtyList = new ArrayList<>();
    private List<GetTripFlowsBySelfEty> getTripFlowsBySelfEtyArrayList = new ArrayList<>();
    private List<GetOverTimeFlowsBySelfEty> getOverTimeFlowsBySelfEtyList = new ArrayList<>();

    @Override
    protected void initTopBar() {

        title = getIntent().getStringExtra("title");
        second=getIntent().getBooleanExtra("push",false);

        setTitle(title);
        enableNavLeftImg();
        enableNavRightImg(R.drawable.icon_add);
    }

    @Override
    protected void initLayoutDate() {
        resMoreSendFragment=new ResMoreSendFragment(getIntent().getStringExtra("title"));
        resMoreReciveFragment=new ResMoreReciveFragment(getIntent().getStringExtra("title"));
        mFragmentList.add(resMoreSendFragment);
        mFragmentList.add(resMoreReciveFragment);
        relvef();
    }

    @Override
    protected void initAdapter() {

    }

    public void relvef(){
        if(TextUtils.equals(title, "请假管理")){
            doRequestHttp(new GetLeaveFlowsBySelfReq("1"));
        }

        if(TextUtils.equals(title,"出差管理")){
            doRequestHttp(new GetTripFlowsBySelfReq("1"));
        }

        if(TextUtils.equals(title,"加班管理")){
            doRequestHttp(new GetOverTimeFlowsBySelfReq("1"));
        }

        if(TextUtils.equals(title,"报销审批")){
            String[] mTitleArr = {"我发出的", "我审批的"};
            setFragmentList(mFragmentList, mTitleArr);
        }
    }

    public int setCheck(String tag,Object object){
        int i=0;
        switch (tag) {
            case "GetLeaveFlowsBySelfReq":
                List<GetLeaveFlowsBySelfEty> getLeaveFlowsBySelfEtyListResult = (List<GetLeaveFlowsBySelfEty>) object;
                for (GetLeaveFlowsBySelfEty getLeaveFlowsBySelfEty:getLeaveFlowsBySelfEtyListResult){
                    if (!("归档".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "失败".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批不通过".equals(getLeaveFlowsBySelfEty.getWorkFlow().getNodeName()) || getLeaveFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId()))
                    {
                        i++;
                    }
                }
                return i;
            case "GetTripFlowsBySelfReq":
                List<GetTripFlowsBySelfEty> getTripFlowsBySelfEtyArrayListResult = (List<GetTripFlowsBySelfEty>) object;
                for (GetTripFlowsBySelfEty getTripFlowsBySelfEty:getTripFlowsBySelfEtyArrayListResult){
                    if (!("归档".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "失败".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批不通过".equals(getTripFlowsBySelfEty.getWorkFlow().getNodeName()) || getTripFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId()))
                    {
                        i++;
                    }
                }
                return i;
            case "GetOverTimeFlowsBySelfReq":
                List<GetOverTimeFlowsBySelfEty> getOverTimeFlowsBySelfEtyListResult = (List<GetOverTimeFlowsBySelfEty>) object;
                for (GetOverTimeFlowsBySelfEty getOverTimeFlowsBySelfEty:getOverTimeFlowsBySelfEtyListResult){
                    if (!("归档".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "失败".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || "审批不通过".equals(getOverTimeFlowsBySelfEty.getWorkFlow().getNodeName()) || getOverTimeFlowsBySelfEty.getNextUserId() != WorkUIKit.getUser().getId()))
                    {
                        i++;
                    }
                }
                return i;
        }
        return i;
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        Log.e("ResponseSuccess", tag + "");
        super.onResponseSuccess(response, tag);
        int i=setCheck(tag,response.getReturnObject());
        if (f){
            if (i>0){
                String[] mTitleArr = {"我发出的", "我审批的("+i+")"};
                setFragmentList(mFragmentList, mTitleArr);
                f=false;
            }
            else {
                String[] mTitleArr = {"我发出的", "我审批的"};
                setFragmentList(mFragmentList, mTitleArr);
                f=false;
            }
        }
        else {
            if (i>0){
                String[] mTitleArr = {"我发出的", "我审批的("+i+")"};
                reslf(mTitleArr);
            } else {
                String[] mTitleArr = {"我发出的", "我审批的"};
                reslf(mTitleArr);
            }
        }
        if (second){
           setDefaultPage(1);
        }
    }

    @Override
    protected void initListener() {
        setOnNavRightClickListener(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK
            case 1001:
                resMoreSendFragment.relvef();
                break;
            case 1002:
                relvef();
                resMoreReciveFragment.relvef();
                break;
            default:
                break;
        }
    }

    @Override
    public void onNavRightClick() {
        if (TextUtils.equals(title, "请假管理")) {
            intent3Acticity(ResLeaveActivity.class);
        } else if (TextUtils.equals(title, "出差管理")) {
            intent3Acticity(ResOutActivity.class);
        } else if(TextUtils.equals(title, "加班管理")){
            intent3Acticity(ResOverWorkActivity.class);
        } else if (TextUtils.equals(title, "报销审批")) {
            intent2Acticity(ResReviewActivity.class);
        }
    }
}
